package auto.feature;


import auto.device.Phione;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.security.SecureRandom;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//此处方法用来解决群聊中除转发外的长按选项
public class GroupLongPressMessageOptions {

//feature1：复制粘贴
    public String CopyAndPaste() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击联系人，进入联系人列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeButton[`name == \"联系人\"`]"))).click();

        //点击我的群组
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("我的群组"))).click();

        //选中第一个群聊
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]"))).click();

        //定位文本框元素
        WebElement textBox=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("输入消息")));
        textBox.click();
        textBox.sendKeys("测试复制粘贴文本");

        //点击发送
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._sendButton"))).click();
        Thread.sleep(3000);

        //定位刚才发送的文本信息
        List<WebElement> textMessages=driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeOther[`name MATCHES '您发送： 测试复制粘贴文本.*'`]"));

        WebElement newTextMessage=textMessages.get(textMessages.size()-1);
        //创建长按手势
        Actions actions = new Actions(driver);

        // 执行长按操作
        actions.clickAndHold(newTextMessage)
                .pause(Duration.ofSeconds(2))
                .release()
                .build()
                .perform();

        //点击选项中的复制文字选项
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("复制文字"))).click();
        Thread.sleep(1000);

        //点击文本框并点击粘贴
        textBox.click();
        Thread.sleep(1000);
        //尝试点击出现的粘贴，出现后就是正常的
        try{
            //尝试执行点击粘贴的操作
            driver.findElement(AppiumBy.accessibilityId("粘贴")).click();
            System.out.println("已经点击粘贴，请观察");
        }
        catch(NoSuchElementException e){
            System.out.println("粘贴选项没有出现");
            return "粘贴选项没有出现";
        }

        //将粘贴的内容发送
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._sendButton"))).click();
        Thread.sleep(3000);

        //如果这个相同的消息数量增加，就证明复制粘贴成功
        List<WebElement> newTextMessages=driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeOther[`name MATCHES '您发送： 测试复制粘贴文本.*'`]"));
        if(newTextMessages.size()==textMessages.size()){
            return "粘贴后的内容不一致";
        }

        //最后点击返回到主页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon back blue"))).click();
        System.out.println("复制粘贴成功");
        driver.quit();
        return "复制粘贴功能正常";
    }


//feature2:保存到我的存储
    public String saveStorage() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击联系人，进入联系人列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeButton[`name == \"联系人\"`]"))).click();

        //点击我的群组
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("我的群组"))).click();

        //选中第一个群聊
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]"))).click();

        String str="测试保存到存储选项文本"+generateRandomString();
        //定位文本框元素
        WebElement textBox=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("输入消息")));
        textBox.click();
        textBox.sendKeys(str);

        //点击发送
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._sendButton"))).click();
        Thread.sleep(3000);

        //定位刚才发送的文本信息
        List<WebElement> textMessages=driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeOther[`name MATCHES '您发送： "+str+".*'`]"));

        WebElement newTextMessage=textMessages.get(textMessages.size()-1);
        //创建长按手势
        Actions actions = new Actions(driver);

        // 执行长按操作
        actions.clickAndHold(newTextMessage)
                .pause(Duration.ofSeconds(2))
                .release()
                .build()
                .perform();

        //点击选项中的保存到存储选项
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("保存到存储"))).click();
        Thread.sleep(2000);

        //这地方要判断是不是登录后第一次使用存储,或者是账号首次使用存储。如果没有进入这个界面就是之前已经使用过
        try{
            driver.findElement(AppiumBy.accessibilityId("我的存储"));

            //点击创建存储，或者打开存储
            driver.findElement(AppiumBy.xpath("//XCUIElementTypeStaticText[@name='创建存储' or @name='打开存储']")).click();
            WebElement one=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("1")));
            //默认密码 111111
            for(int i=1;i<=6;i++){
                one.click();
            }
            Thread.sleep(2000);

            //如果从来没有使用过，还要再次输入确认密码
            if(driver.findElements(AppiumBy.accessibilityId("2.circle")).size()>0){
                WebElement newOne=driver.findElement(AppiumBy.accessibilityId("1"));
                //默认密码 111111
                for(int i=1;i<=6;i++){
                    newOne.click();
                }
                Thread.sleep(2000);
            }
        }
        catch(NoSuchElementException e){
            System.out.println("已经点击保存到存储，请观察提示");
        }

        //点击返回查看
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon back blue"))).click();

        //点击导航栏-聊天
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"聊天\"`]"))).click();

        //点击我的存储
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("我的存储"))).click();
        Thread.sleep(3000);

        //进入后查看是不是有保存的那个字符串
        List<WebElement> newTextMessages=driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeOther[`name MATCHES '您发送： "+str+".*'`]"));
        if(newTextMessages.size()>0){
        }
        else{
            System.out.println("保存到存储失败");
            return "保存到存储失败";
        }

        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon back blue"))).click();
        driver.quit();
        System.out.println("保存到存储成功");
        return "保存到存储成功";
    }

//随机出来一个字符串，用于存储中使用
    public static String generateRandomString() {
        // 定义字符集，包括大写字母、小写字母和数字
        final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        final int LENGTH = 6; // 生成字符串的长度

        SecureRandom random = new SecureRandom(); // 使用 SecureRandom 以增强随机性
        StringBuilder sb = new StringBuilder(LENGTH);

        for (int i = 0; i < LENGTH; i++) {
            int index = random.nextInt(CHARACTERS.length()); // 从字符集中随机选择一个字符
            sb.append(CHARACTERS.charAt(index));
        }

        return sb.toString(); // 返回生成的随机字符串
    }



//feature3：群聊-举报
    public String report() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击联系人，进入联系人列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeButton[`name == \"联系人\"`]"))).click();

        //点击我的群组
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("我的群组"))).click();

        //选中第一个群聊
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]"))).click();

        System.out.println("请在30秒内让对方发送消息，或者确认当前页面存在对方发送的消息");
        WebElement message=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.xpath
                ("//XCUIElementTypeOther[contains(@name, '发送：') and not(contains(@name, '您'))]")));

        //对这个元素进行长按
        Actions actions = new Actions(driver);

        // 执行长按操作
        actions.clickAndHold(message)
                .pause(Duration.ofSeconds(2))
                .release()
                .build()
                .perform();

        //点击举报选项
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("举报"))).click();

        //点击垃圾信息选项
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("垃圾信息"))).click();

        //在证据描述文本框中添加描述
        WebElement textBox=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.className("XCUIElementTypeTextView")));
        textBox.click();
        textBox.sendKeys("对方发送了垃圾消息");

        //点击完成收回键盘
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("Done"))).click();

        //向上滑动一下
        Map<String, Object> argsUp = new HashMap<>();
        argsUp.put("direction", "up");
        driver.executeScript("mobile: swipe", argsUp);

        //点击照片，添加一张照片
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon addimg"))).click();
        Thread.sleep(1000);

        //选择最后一张照片
        List<WebElement> pictures=driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"zl btn unselected\"`]"));
        pictures.get(pictures.size()-1).click();

        //点击之后点击确定
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"确定(1)\"`]"))).click();

        //点击提交举报
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"提交举报\"`]"))).click();
        Thread.sleep(2000);

        if(driver.findElements(AppiumBy.accessibilityId("icon back blue")).size()==0){
            System.out.println("提交举报失败");
            return "提交举报失败";
        }

        //举报之后点击返回主界面
        driver.findElement(AppiumBy.accessibilityId("icon back blue")).click();
        driver.quit();
        System.out.println("举报完成");
        return "举报对方成功";
    }



//feature4：群聊-删除消息
    public String delete() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击联系人，进入联系人列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeButton[`name == \"联系人\"`]"))).click();

        //点击我的群组
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("我的群组"))).click();

        //选中第一个群聊
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]"))).click();

        //定位文本框元素
        WebElement textBox=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("输入消息")));
        textBox.click();
        textBox.sendKeys("测试删除选项的文本");

        //点击发送
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._sendButton"))).click();
        Thread.sleep(3000);

        //定位刚才发送的文本信息
        List<WebElement> textMessages=driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeOther[`name MATCHES '您发送： 测试删除选项的文本.*'`]"));

        WebElement newTextMessage=textMessages.get(textMessages.size()-1);
        //创建长按手势
        Actions actions = new Actions(driver);

        // 执行长按操作
        actions.clickAndHold(newTextMessage)
                .pause(Duration.ofSeconds(2))
                .release()
                .build()
                .perform();

        //出现选项后点击删除选项
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("删除"))).click();

        //点击为所有成员删除
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("为所有成员删除"))).click();

        //判断发送的那条消息的数量有没有减少
        List<WebElement> newTextMessages=driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeOther[`name MATCHES '您发送： 测试删除选项的文本.*'`]"));
        if(newTextMessages.size()==textMessages.size()){
            System.out.println("删除消息失败");
            return "删除消息失败";
        }

        driver.findElement(AppiumBy.accessibilityId("icon back blue")).click();
        driver.quit();
        System.out.println("删除完成");
        return "成功为所有成员删除此条信息";
    }
}
